package com.bjpowernode.pay.controller;

import cn.hutool.core.util.StrUtil;
import com.bjpowernode.mvc.model.dto.user.TokenUserDTO;
import com.bjpowernode.mvc.model.response.Response;
import com.bjpowernode.pay.model.dto.KuaiQianFormDTO;
import com.bjpowernode.pay.model.param.AutoLoginParam;
import com.bjpowernode.pay.model.param.NotifyParam;
import com.bjpowernode.pay.service.PayService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;


@Controller
public class PayController {

    @Resource
    private PayService payService;

    @RequestMapping(value = "/pay/kuaiqian/input")
    public String payInput(@RequestParam Integer uid,
                           @RequestParam String token,
                           @RequestParam BigDecimal money,
                           Model model) {
        String view = "/pay/error";

        try {
            //检查请求参数.
            if (uid != null && uid > 0 && StrUtil.isNotBlank(token) && (money != null && money.doubleValue() > 0)) {

                //检查token与uid是否正确
                boolean checkToken = payService.checkToken(uid, token);
                if (checkToken) {

                    //生成所需要的参数.
                    KuaiQianFormDTO formDTO = payService.createApiResuestData(uid, token, money);
                    model.addAttribute("kq", formDTO);


                    //调用方法,保存充值记录.
                    payService.saveRecharge(uid, formDTO.getOrderId(), money);

                    //将orderId存入redis,为了后面的订单轮询使用
                    payService.saveOrderRedis(formDTO.getOrderId());

                    //forward到Thymeleaf
                    view = "/pay/kuaiQian";
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }


    //异步通知.
    @GetMapping(value = "/pay/kuaiqian/notice")
    @ResponseBody
    public String receiveNotice(NotifyParam notifyParam) {
        try {
            //快钱参数.
//            验证签名.
            boolean flag = notifyParam.checkSign();
            System.out.println("验证签名:接收异步通知的代码");
            if (flag) {
                //签名正确,判断商户号
                if ("1001214035601".equals(notifyParam.getMerchantAcctId())) {
                    //可以处理.
                    String result = payService.handLeKuaiQianNotify(notifyParam.getOrderId(),
                            notifyParam.getPayAmount(), notifyParam.getPayResult());

                    System.out.println(notifyParam.getOrderId() + "订单处理完成: " + result);
                } else {
                    //模拟日志.
                    System.out.println("订单号: " + notifyParam.getOrderId() + ",商户号错误: " + notifyParam.getMerchantAcctId());
                }
            } else {
                //模拟日志.
                System.out.println("订单号: " + notifyParam.getOrderId() + ",签名错误: " + notifyParam.getMerchantAcctId());

            }
            //从redis删除已经处理过的订单号
            if (StrUtil.isNotBlank(notifyParam.getOrderId())) {

                payService.removeOrderIdRedis(notifyParam.getOrderId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        //返回信息.
        return "<result>1</result><redirecturl>http://localhost:8080/pay/result</redirecturl>";
    }


    //充值成功,跳转用户中心
    @PostMapping(value = "/pay/auto/login")
    @ResponseBody
    public Response autoLogin(AutoLoginParam autoLoginParam) {

        boolean checkToken = payService.checkToken(autoLoginParam.getUid(), autoLoginParam.getToken());

        if (checkToken) {
            //存储token到redis
            Map<String, String> data = payService.getLoginTokenInfoFromRedis(autoLoginParam.getToken());
            return Response.ok(data);
        }
        return Response.FAIL();
    }


}
